//
//@File    :	12.2_strngbad.cpp
//@Time    :	2021/09/01 11:30:51
//@Auther  :	Sun Sheng
//@Contact :	970340843@qq.com
//@Desc    :	StringBad class methods
//

#include <cstring>
#include "12.1_strngbad.h"
using std::cout;

//initializing static class member
int StringBad::num_strings=0;
//不能在类声明中初始化静态成员变量
//对于静态类成员,可以在类声明之外使用单独的语句来初始化
//!初始化语句指出了类型,使用了作用域运算符,未使用static关键字
//在方法文件中,为不是类声明文件中进行
//class methods
//constructor StringBad from C string
StringBad::StringBad(const char * s)
{
    len=std::strlen(s);//set size
    str=new char[len+1];//allot storage
    std::strcpy(str,s);//initializze pointer
    num_strings++;//set object count
    cout<<num_strings<<":\""<<str<<"\"object created\n";//For Your Information
}

StringBad::StringBad()//default constructor
{
    len=4;
    str=new char[4];
    std::strcpy(str,"C++");//default string
    num_strings++;
    cout<<num_strings<<":\""<<str<<"\"default object created\n";//For Your Information
}

StringBad::~StringBad()//necessary destructor
{
    cout<<":\""<<str<<"\"object created, ";//For Your Information
    --num_strings;//required
    cout<<num_strings<<" left\n";//FYI
    delete [] str;
}
std::ostream & operator<<(std::ostream & os,const StringBad & st)
{
    os<<st.str;
    return os;
}